(function() {
  define(['handlebars', 'chaplin', 'lib/utils'], function(Handlebars, Chaplin, utils) {
    'use strict';
    var formatOptions, formatPrefixAndSuffix;
    formatOptions = function(options) {
      var attrs, defaults, formGroupClass, key, labelAttrs, value;
      defaults = {
        label: 'Label'
      };
      _.defaults(options, defaults);
      attrs = [];
      labelAttrs = [];
      formGroupClass = 'form-group';
      if (options.className) {
        options.className += ' form-control';
      } else {
        options.className = 'form-control';
      }
      attrs.push("class=\"" + options.className + "\"");
      delete options.className;
      if (!options.required) {
        delete options.required;
      } else {
        options.label += "<em>*</em>";
      }
      if (options.labelClass) {
        labelAttrs.push("class=\"control-label " + options.labelClass + "\"");
        delete options.labelClass;
      } else {
        labelAttrs.push('class="control-label"');
      }
      if (options.id != null) {
        attrs.push("id=\"" + options.id + "\"");
        labelAttrs.push("for=\"" + options.id + "\"");
        delete options.id;
      }
      if (options.formGroupClass) {
        formGroupClass += " " + options.formGroupClass;
      }
      for (key in options) {
        value = options[key];
        attrs.push("" + key + "=\"" + value + "\"");
      }
      delete attrs.label;
      return [attrs, labelAttrs, formGroupClass];
    };
    formatPrefixAndSuffix = function(options, input) {
      var str;
      str = "";
      if ((options.prefix != null) && (options.suffix != null)) {
        str += "<div class=\"input-prepend input-append\">\n  <span class=\"add-on\">" + options.prefix + "</span>" + input + "<span class=\"add-on\">" + options.suffix + "</span>\n</div>";
      } else if (options.prefix != null) {
        str += "<div class=\"input-prepend\">\n  <span class=\"add-on\">" + options.prefix + "</span>" + input + "\n</div>";
      } else if (options.suffix != null) {
        str += "<div class=\"input-append\">\n  " + input + "<span class=\"add-on\">" + options.suffix + "</span>\n</div>";
      } else {
        str += "" + input;
      }
      return str;
    };
    Handlebars.registerHelper('input', function(options) {
      var attrs, formGroupClass, labelAttrs, str, value, _ref;
      _ref = formatOptions(options.hash), attrs = _ref[0], labelAttrs = _ref[1], formGroupClass = _ref[2];
      value = options.fn != null ? options.fn(this) : '';
      str = "<div class=\"" + formGroupClass + "\">\n  <label " + (labelAttrs.join(' ')) + ">" + options.hash.label + "</label>";
      str += formatPrefixAndSuffix(options.hash, "<input " + (attrs.join(' ')) + " />");
      str += "  " + ($.trim(value)) + "\n</div>";
      return new Handlebars.SafeString(str);
    });
    Handlebars.registerHelper('textareaInput', function(options) {
      var attrs, formGroupClass, labelAttrs, str, value, _ref;
      _ref = formatOptions(options.hash), attrs = _ref[0], labelAttrs = _ref[1], formGroupClass = _ref[2];
      value = options.fn != null ? options.fn(this) : '';
      str = "<div class=\"" + formGroupClass + "\">\n  <label " + (labelAttrs.join(' ')) + ">" + options.hash.label + "</label>";
      str += formatPrefixAndSuffix(options.hash, "<textarea " + (attrs.join(' ')) + "></textarea>");
      str += "  " + ($.trim(value)) + "\n</div>";
      return new Handlebars.SafeString(str);
    });
    Handlebars.registerHelper('selectInput', function(context, options) {
      var attrs, body, createItem, empty, formGroupClass, key, label, labelAttrs, str, _i, _len, _ref, _ref1, _ref2;
      if (((_ref = options.hash) != null ? _ref.empty : void 0) != null) {
        empty = (_ref1 = options.hash) != null ? _ref1.empty : void 0;
        delete options.hash.empty;
      }
      _ref2 = formatOptions(options.hash), attrs = _ref2[0], labelAttrs = _ref2[1], formGroupClass = _ref2[2];
      body = options.fn != null ? options.fn(this) : '';
      str = "<div class=\"" + formGroupClass + "\">\n  <label " + (labelAttrs.join(' ')) + ">" + options.hash.label + "</label>\n  <select " + (attrs.join(' ')) + ">";
      if (empty != null) {
        str += "<option value=\"\">" + empty + "</option>";
      }
      if (context != null) {
        createItem = function(key, label) {
          var k, l, o;
          if (_.isObject(label)) {
            o = label;
            for (k in o) {
              l = o[k];
              key = k;
              label = l;
            }
          }
          return '<option value="' + key + '">' + label + '</option>';
        };
        if (_.isArray(context)) {
          for (key = _i = 0, _len = context.length; _i < _len; key = ++_i) {
            label = context[key];
            str += createItem(key, label);
          }
        } else if (_.isObject(context)) {
          for (key in context) {
            label = context[key];
            str += createItem(key, label);
          }
        }
      }
      str += "  </select>\n  " + body + "\n</div>";
      return new Handlebars.SafeString(str);
    });
    Handlebars.registerHelper('checkboxGroup', function(context, options) {
      var attrs, body, createItem, formGroupClass, key, label, labelAttrs, str, _i, _len, _ref;
      _ref = formatOptions(options.hash), attrs = _ref[0], labelAttrs = _ref[1], formGroupClass = _ref[2];
      body = options.fn != null ? options.fn(this) : '';
      str = "<div class=\"" + formGroupClass + "\">\n  <label " + (labelAttrs.join(' ')) + ">" + options.hash.label + "</label>\n  <div class=\"form-control height-auto\">";
      if (context != null) {
        createItem = function(name, key, label) {
          var k, l, o;
          if (name == null) {
            name = '';
          }
          if (_.isObject(label)) {
            o = label;
            for (k in o) {
              l = o[k];
              key = k;
              label = l;
            }
          }
          return "<div class=\"checkbox\">\n  <label>\n    <input type=\"checkbox\" value=\"" + key + "\" name=\"" + name + "\" />\n    " + label + "\n  </label>\n</div>";
        };
        if (_.isArray(context)) {
          for (key = _i = 0, _len = context.length; _i < _len; key = ++_i) {
            label = context[key];
            str += createItem(options.hash.name, key, label);
          }
        } else if (_.isObject(context)) {
          for (key in context) {
            label = context[key];
            str += createItem(options.hash.name, key, label);
          }
        }
      }
      str += "  " + body + "\n  </div>\n</div>";
      return new Handlebars.SafeString(str);
    });
    Handlebars.registerHelper('radioGroup', function(context, options) {
      var attrs, body, createItem, formGroupClass, isInline, key, label, labelAttrs, str, _i, _len, _ref;
      if (options.hash.isInline) {
        isInline = true;
        delete options.hash.isInline;
      }
      _ref = formatOptions(options.hash), attrs = _ref[0], labelAttrs = _ref[1], formGroupClass = _ref[2];
      body = options.fn != null ? options.fn(this) : '';
      str = "<div class=\"" + formGroupClass + "\">\n  <label " + (labelAttrs.join(' ')) + ">" + options.hash.label + "</label>\n  " + (isInline ? '<br />' : '') + "\n  <div class=\"form-control height-auto\">";
      if (context != null) {
        createItem = function(name, key, label) {
          var k, l, o, strItem;
          if (name == null) {
            name = '';
          }
          if (_.isObject(label)) {
            o = label;
            for (k in o) {
              l = o[k];
              key = k;
              label = l;
            }
          }
          strItem = '';
          if (!isInline) {
            strItem += '<div class="radio">';
          }
          strItem += "<label " + (isInline ? 'class="radio-inline"' : '') + ">\n  <input type=\"radio\" value=\"" + key + "\" name=\"" + name + "\" />\n  " + label + "\n</label>";
          if (!isInline) {
            strItem += '</div>';
          }
          return strItem;
        };
        if (_.isArray(context)) {
          for (key = _i = 0, _len = context.length; _i < _len; key = ++_i) {
            label = context[key];
            str += createItem(options.hash.name, key, label);
          }
        } else if (_.isObject(context)) {
          for (key in context) {
            label = context[key];
            str += createItem(options.hash.name, key, label);
          }
        }
      }
      str += "    " + body + "\n  </div>\n</div>";
      return new Handlebars.SafeString(str);
    });
    Handlebars.registerHelper('buttons', function(options) {
      var formGroupClass, str;
      _.defaults(options.hash, {
        submitLabel: 'Cadastrar',
        cancelLabel: 'Voltar'
      });
      formGroupClass = 'form-group';
      if (options.hash.formGroupClass) {
        formGroupClass += " " + options.hash.formGroupClass;
      }
      str = "<div class=\"" + formGroupClass + "\">";
      str += "<button type=\"submit\" class=\"btn btn-primary pull-right\">" + options.hash.submitLabel + "</button>";
      if (options.hash.cancelHref != null) {
        str += "<a class=\"btn btn-default pull-right\" " + ' ' + "href=\"" + options.hash.cancelHref + "\" style=\"margin-right:5px;\">" + options.hash.cancelLabel + "</a>";
      }
      str += "</div>";
      return new Handlebars.SafeString(str);
    });
    Handlebars.registerHelper('formGroup', function(options) {
      var attrs, body, formGroupClass, labelAttrs, str, _ref;
      _ref = formatOptions(options.hash), attrs = _ref[0], labelAttrs = _ref[1], formGroupClass = _ref[2];
      body = options.fn != null ? options.fn(this) : '';
      str = "<div class=\"" + formGroupClass + "\">\n  <label " + (labelAttrs.join(' ')) + ">" + options.hash.label + "</label>\n  " + body + "\n</div>";
      return new Handlebars.SafeString(str);
    });
    return null;
  });

}).call(this);
